package com.fei.yuan.myapplication.utils;

import com.fei.yuan.myapplication.ResourceTable;
import com.fei.yuan.myapplication.database.FormBean;
import ohos.agp.components.ComponentProvider;
import ohos.app.Context;

import java.util.Calendar;

/**
 * @since 2021-07-21
 */
public class ComponentProviderUtils {
    private static Context mContext;
    private static final int DIMENSION_1X2 = 1;
    private static final int DIMENSION_2X2 = 2;
    private static final int DIMENSION_2X4 = 3;
    private static final int DIMENSION_4X4 = 4;

    /**
     * 获取ComponentProvider
     *
     * @param formBean
     * @param context
     * @return ComponentProvider
     */
    public static ComponentProvider getComponentProvider(FormBean formBean, Context context){
        mContext = context;
        int layoutId = ResourceTable.Layout_form_image_with_info_time_2_2;
        if (formBean.getDimension() == DIMENSION_2X4){
            layoutId = ResourceTable.Layout_form_image_with_info_time_2_4;
        }else if (formBean.getDimension() == DIMENSION_1X2){
            layoutId = ResourceTable.Layout_form_image_with_info_time_1_2;
        }
        ComponentProvider componentProvider =new ComponentProvider(layoutId,context);
        setComponentProvider(componentProvider);
        return componentProvider;
    }

    /**
     * 设置信息
     *
     * @param componentProvider
     */
    private static void setComponentProvider(ComponentProvider componentProvider) {
        Calendar instance = Calendar.getInstance();
        // 时分秒
        int hour = instance.get(Calendar.HOUR_OF_DAY);
        int min = instance.get(Calendar.MINUTE);
        int second = instance.get(Calendar.SECOND);
        componentProvider.setText(ResourceTable.Id_hour,intToString(hour));
        componentProvider.setText(ResourceTable.Id_min,intToString(min));
        componentProvider.setText(ResourceTable.Id_second,intToString(second));

        // 星期
        int week = instance.get(Calendar.DAY_OF_WEEK);
        String weekString = getWeek(week);
        componentProvider.setText(ResourceTable.Id_week,weekString);

        // 日期
        String currentDate = DateUtils.getCurrentDate("yyyy-MM-dd");
        String[] split = currentDate.split("-");
        componentProvider.setText(ResourceTable.Id_month,split[1]);
        componentProvider.setText(ResourceTable.Id_day,split[2]);

    }

    public static String intToString(int time) {
        if (String.valueOf(time).length() < 2){
            return "0"+time;
        }else {
            return time+"";
        }
    }

    //获取星期
    public static String getWeek(int week) {

        int stringId;
        switch (week) {
            case 1:
                stringId = ResourceTable.String_SUNDAY;
                break;
            case 2:
                stringId = ResourceTable.String_MONDAY;
                break;
            case 3:
                stringId = ResourceTable.String_TUESDAY;
                break;
            case 4:
                stringId = ResourceTable.String_WEDNESDAY;
                break;
            case 5:
                stringId = ResourceTable.String_THURSDAY;
                break;
            case 6:
                stringId = ResourceTable.String_FRIDAY;
                break;
            case 7:
                stringId = ResourceTable.String_SATURDAY;
                break;
            default:
                stringId = ResourceTable.String_SUNDAY;
                break;
        }
        return mContext.getString(stringId);
    }
}
